//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import ar.com.ford.it.webengine.websession.WebSession;
import ar.com.ford.it.webengine.websession.WebSessionHelper;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;


/**
 * WebSessionListener <br>
 * Listener que atiende los eventos de creacion y destruccion de la sesion http. <br>
 * 
 * Al destruirse la sesion http tambien se destruye la
 * sesion interna(WebSession) correspondiente.
 * 
 * @author Aji
 */
public class WebSessionListener implements HttpSessionListener {

	//private HttpSession session;


    private static final String CLASS_NAME = WebSessionListener.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);

	/**
	 * @see javax.servlet.http.HttpSessionListener#sessionCreated(HttpSessionEvent)
	 */
	public void sessionCreated(HttpSessionEvent event) {
		String METHOD_NAME="sessionCreated";
		
		log.entering(CLASS_NAME, METHOD_NAME);
		//this.session = event.getSession();		
		log.exiting(CLASS_NAME, METHOD_NAME);
		
	}

	/**
	 * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(HttpSessionEvent)
	 */
	public void sessionDestroyed(HttpSessionEvent event) {
		String METHOD_NAME = "sessionDestroyed";
		
		log.entering(CLASS_NAME, METHOD_NAME);
		
		try {
			HttpSession httpSession = event.getSession();
			
			WebSession webSession = WebSessionHelper.removeWebSession(httpSession);

			if (webSession != null)
				log.logp(Level.FINE, CLASS_NAME, METHOD_NAME, "Sesion expirada", webSession.getUser().getId());
			
		}
		catch (Exception e) {
			/*
			 * Session already invalidated
			 * No se puede tratar esta exception
			 */
		}
		
		log.exiting(CLASS_NAME, METHOD_NAME);									
	}

}
